<template>
  <div class="mod-role">
    <el-form :inline="true" :model="roleDto" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="roleDto.roleName" placeholder="角色名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button v-if="isAuth('sys:role:list')" type="success" @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('sys:role:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('sys:role:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle" style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="sort" header-align="center" align="center" width="120" label="权限排序"></el-table-column>
      <el-table-column prop="roleName" header-align="center" align="center" label="角色名称"></el-table-column>
      <el-table-column prop="roleCode" header-align="center" align="center" label="角色编码"></el-table-column>
      <el-table-column prop="remark" header-align="center" align="center" label="备注"></el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" width="200" label="创建时间"></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="300" label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('sys:role:update')" type="primary" icon="el-icon-edit" size="mini" @click="addOrUpdateHandle(scope.row.id)"></el-button>
          <el-button v-if="isAuth('sys:role:delete')" type="danger" icon="el-icon-delete" size="mini" @click="deleteHandle(scope.row.id)"></el-button>
          <el-button v-if="isAuth('sys:role:update')" type="info" icon="el-icon-arrow-up" size="mini" @click="changeSort(scope.row.id,'asc')"></el-button>
          <el-button v-if="isAuth('sys:role:update')" type="info" icon="el-icon-arrow-down" size="mini" @click="changeSort(scope.row.id,'desc')"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination  @size-change="sizeChangeHandle"  @current-change="currentChangeHandle" :current-page="roleDto.pageNum"
      :page-sizes="[10, 20, 50, 100]" :page-size="roleDto.pageSize" :total="totalPage" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './role-add-or-update'
  export default {
    data () {
      return {
        roleDto: {
          roleName: '',
          pageNum: 1,
          pageSize: 10
        },
        dataList: [],
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/admin-login/role/queryRoles'),
          method: 'post',
          data: this.roleDto
        }).then(({data}) => {
          if (data.code === 200) {
            this.dataList = data.data.records
            this.totalPage = data.data.total
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数量变化
      sizeChangeHandle (val) {
        this.roleDto.pageSize = val
        this.roleDto.pageNum = 1
        this.getDataList()
      },
      // 跳转指定页数
      currentChangeHandle (val) {
        this.roleDto.pageNum = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        const foundItem = this.dataList.find(item => item.id === id)
        var roleNames = id ? [foundItem.roleName] : this.dataListSelections.map(item => {
          return item.roleName
        })
        this.$confirm(`是否对【${roleNames.join('、')}】进行${id ? '删除' : '批量删除'}?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/admin-login/role/deleteRole'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data.code === 200 && data.data === true) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message({
                showClose: true,
                message: data.message,
                type: 'warning',
                center: true
              })
            }
          })
        }).catch(() => {})
      },
      // 权限排序，升序/降序
      changeSort (id, direction) {
        this.$http({
          url: this.$http.adornUrl(`/admin-login/role/changeSort/${id}/${direction}`),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 200) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message({
              showClose: true,
              message: data.message,
              type: 'warning',
              center: true
            })
          }
        })
      }
    }
  }
</script>
